package com.common.utils;

import io.jsonwebtoken.*;

public class jwtUtil {
    public static String  createJwt(){
        JwtBuilder jwtBuilder = Jwts.builder();
        String jwtToken = jwtBuilder
                //header
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS512")
                //payload
                .claim("username","jwq")
                .claim("role","admin")
                //signature
                .signWith(SignatureAlgorithm.HS512,"admin")
                .compact();
        return jwtToken;
    }

    public static boolean parse(String token){
        if(token==null)
        {
            return false;
        }
        try{
            Jws<Claims> claimsJws = Jwts.parser().setSigningKey("admin").parseClaimsJws(token);
        }catch(Exception e)
        {
            System.out.println("验证失败");
            return false;
        }
        return true;
    }
}
